<template>
  <transition name="fade">
    <div v-if="showPopup" class="idle-logout">
      <div class="idle-logout__overlay" @click="cancelLogout"></div>
      <div class="idle-logout__modal" @click.stop>
        <div class="idle-logout__header">
          <i class="idle-logout__icon">⚠️</i>
          <h2 class="idle-logout__title">温馨提示</h2>
        </div>
        <div class="idle-logout__content">
          <p class="idle-logout__message">
            您已超过<span class="idle-logout__highlight">{{ idleTimeInSeconds }}</span>秒未操作，
            系统将在<span class="idle-logout__countdown">{{ countdown }}</span>秒后自动退出返回首页。
          </p>
          <div class="idle-logout__progress-container">
            <div class="idle-logout__progress" :style="{ width: `${(countdown / 10) * 100}%` }"></div>
          </div>
        </div>
        <div class="idle-logout__footer">
          <button class="idle-logout__btn idle-logout__btn--cancel" @click="cancelLogout">继续操作</button>
          <button class="idle-logout__btn idle-logout__btn--logout" @click="logoutNow">返回首页</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup>

const props = defineProps({
  idleTime: {
    type: Number,
    default: 3 * 60 * 1000 // 默认三分钟
  }
});

// 计算闲置时间（秒）用于显示
const idleTimeInSeconds = Math.floor(props.idleTime / 1000);

// 响应式变量
const lastActionTime = ref(new Date());
const countdown = ref(10);
const showPopup = ref(false);

// 计时器
let timer = null;
let countdownTimer = null;

/**
 * 更新最后操作时间并重新设置定时器
 */
function updateLastAction() {
  // 更新最后操作时间戳
  lastActionTime.value = new Date();

  // 如果存在定时器，则清除它
  if (timer) clearTimeout(timer);

  // 如果存在倒计时计时器，则清除它
  if (countdownTimer) {
    clearInterval(countdownTimer);
    countdown.value = 10;
    showPopup.value = false;
  }

  // 根据闲置时间重新设置登出定时器
  timer = setTimeout(startLogout, props.idleTime);
}

/**
 * 开始注销过程
 */
function startLogout() {
  console.log('自动退出倒计时开始');
  showPopup.value = true;
  startCountdown();
}

/**
 * 取消注销
 */
function cancelLogout() {
  updateLastAction();
}

/**
 * 立即登出
 */
function logoutNow() {
  clearInterval(countdownTimer);
  showPopup.value = false;
  
  // 执行页面重定向，返回到首页
  uni.reLaunch({
    url: '/pages/index/index'
  });
}

/**
 * 初始化倒计时函数
 */
function startCountdown() {
  countdownTimer = setInterval(() => {
    if (countdown.value > 0) {
      countdown.value--;
    } else {
      countdown.value = 10;
      clearInterval(countdownTimer);
      showPopup.value = false;
      
      // 执行页面重定向，返回到首页
      uni.reLaunch({
        url: '/pages/index/index'
      });
    }
  }, 1000);
}

// 当组件挂载时执行的函数
onMounted(() => {
  updateLastAction();
  // 监听触摸、点击等事件
  const events = ['touchstart', 'mousedown', 'keydown'];
  events.forEach(event => {
    window.addEventListener(event, updateLastAction, { passive: true });
  });
});

// 在组件卸载时执行的清理函数
onUnmounted(() => {
  if (timer) clearTimeout(timer);
  if (countdownTimer) clearInterval(countdownTimer);

  const events = ['touchstart', 'mousedown', 'keydown'];
  events.forEach(event => {
    window.removeEventListener(event, updateLastAction, { passive: true });
  });
});
</script>

<style lang="scss" scoped>
.idle-logout {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
  
  &__overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(3px);
    animation: fade-in 0.4s ease-out;
  }
  
  &__modal {
    position: relative;
    background-color: white;
    border-radius: 16px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
    width: 85%;
    max-width: 400px;
    overflow: hidden;
    animation: popup-appear 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 1;
    transform: translateY(0);
  }
  
  &__header {
    background: linear-gradient(135deg, #f8f9fa, #f1f3f5);
    padding: 18px 20px;
    border-bottom: 1px solid #eee;
    display: flex;
    align-items: center;
  }
  
  &__icon {
    font-size: 26px;
    margin-right: 12px;
    display: flex;
    align-items: center;
    animation: pulse 2s infinite;
  }
  
  &__title {
    margin: 0;
    color: #333;
    font-size: 18px;
    font-weight: 600;
  }
  
  &__content {
    padding: 24px 20px;
  }
  
  &__message {
    margin: 0 0 20px;
    line-height: 1.6;
    color: #444;
    font-size: 15px;
  }
  
  &__highlight {
    color: #1976D2;
    font-weight: bold;
  }
  
  &__countdown {
    font-weight: bold;
    color: #f44336;
    font-size: 20px;
    display: inline-block;
    min-width: 28px;
    text-align: center;
  }
  
  &__progress-container {
    height: 8px;
    background-color: #f0f0f0;
    border-radius: 4px;
    overflow: hidden;
    margin: 18px 0 5px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
  }
  
  &__progress {
    height: 100%;
    background: linear-gradient(90deg, #f44336, #ff7043);
    transition: width 1s linear;
    border-radius: 4px;
  }
  
  &__footer {
    padding: 18px 20px;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    border-top: 1px solid #eee;
    background-color: #fafafa;
  }
  
  &__btn {
    padding: 10px 18px;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
    border: none;
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    
    &--cancel {
      background-color: #f5f5f5;
      color: #333;
      
      &:hover, &:active {
        background-color: #e0e0e0;
        transform: translateY(-1px);
      }
    }
    
    &--logout {
      background: linear-gradient(135deg, #f44336, #e53935);
      color: white;
      
      &:hover, &:active {
        background: linear-gradient(135deg, #e53935, #d32f2f);
        transform: translateY(-1px);
        box-shadow: 0 4px 8px rgba(244, 67, 54, 0.3);
      }
    }
  }
}

// 动画效果
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.4s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

@keyframes popup-appear {
  from {
    transform: scale(0.9) translateY(20px);
    opacity: 0;
  }
  to {
    transform: scale(1) translateY(0);
    opacity: 1;
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}
</style> 